package com.doge.service.admin.config;

import com.doge.bean.pojo.system.System;
import com.doge.mapper.SystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: ConfigServiceImpl
 * @Description:
 * @author: PuJing
 * @create: 2021-11-30-20:49
 **/
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    SystemMapper systemMapper;


    @Override
    public HashMap<String, String> getOrderConfig() {

        String key1 = "market_order_unconfirm";
        String key2 = "market_order_unpaid";
        String key3 = "market_order_comment";

        String value1 = systemMapper.getMarketSystemByKeyName(key1);
        String value2 = systemMapper.getMarketSystemByKeyName(key2);
        String value3 = systemMapper.getMarketSystemByKeyName(key3);

        HashMap<String, String> orderResult = new HashMap<>();

        orderResult.put(key1, value1);
        orderResult.put(key2, value2);
        orderResult.put(key3, value3);

        return orderResult;
    }


    @Override
    public int postConfig(Map map) {

        /*System system1 = new System();
        system1.setKeyName("market_order_unconfirm");
        system1.setKeyValue((String) map.get("market_order_unconfirm"));
        system1.setUpdateTime(new Date());

        systemMapper.setMarketSystemByKeyName(system1);

        System system2 = new System();
        system2.setKeyName("market_order_unpaid");
        system2.setKeyValue((String) map.get("market_order_unpaid"));
        system2.setUpdateTime(new Date());
        systemMapper.setMarketSystemByKeyName(system2);

        System system3 = new System();
        system3.setKeyName("market_order_comment");
        system3.setKeyValue((String) map.get("market_order_comment"));
        system3.setUpdateTime(new Date());
        systemMapper.setMarketSystemByKeyName(system3);*/

        // 前端传入的market_wx_share是Boolean类型，需要转化成String
        if (map.containsKey("market_wx_share")) {

            // 前端也有可能传入String类型，需要排除
            if( !(map.get("market_wx_share") instanceof String)) {

                Boolean market_wx_share = (Boolean) map.get("market_wx_share");

                if (market_wx_share) {
                    map.put("market_wx_share", "true");
                } else {
                    map.put("market_wx_share", "false");
                }
            }
        }

        System system = new System();
        for (Object key : map.keySet()) {

            system.setKeyName((String) key);
            system.setKeyValue((String) map.get(key));
            system.setUpdateTime(new Date());

            systemMapper.setMarketSystemByKeyName(system);
        }


        return 200;
    }


    @Override
    public HashMap<String, String> getMallConfig() {
        String key1 = "market_mall_longitude";
        String key2 = "market_mall_latitude";
        String key3 = "market_mall_address";
        String key4 = "market_mall_qq";
        String key5 = "market_mall_phone";
        String key6 = "market_mall_name";

        String value1 = systemMapper.getMarketSystemByKeyName(key1);
        String value2 = systemMapper.getMarketSystemByKeyName(key2);
        String value3 = systemMapper.getMarketSystemByKeyName(key3);
        String value4 = systemMapper.getMarketSystemByKeyName(key4);
        String value5 = systemMapper.getMarketSystemByKeyName(key5);
        String value6 = systemMapper.getMarketSystemByKeyName(key6);

        HashMap<String, String> orderResult = new HashMap<>();

        orderResult.put(key1, value1);
        orderResult.put(key2, value2);
        orderResult.put(key3, value3);
        orderResult.put(key4, value4);
        orderResult.put(key5, value5);
        orderResult.put(key6, value6);

        return orderResult;
    }

    @Override
    public HashMap<String, String> getExpressConfig() {

        String key1 = "market_express_freight_min";
        String key2 = "market_express_freight_value";

        String value1 = systemMapper.getMarketSystemByKeyName(key1);
        String value2 = systemMapper.getMarketSystemByKeyName(key2);

        HashMap<String, String> expressResult = new HashMap<>();

        expressResult.put(key1, value1);
        expressResult.put(key2, value2);

        return expressResult;


    }

    @Override
    public HashMap<String, String> getWxConfig() {
        String key1 = "market_wx_index_new";
        String key2 = "market_wx_index_topic";
        String key3 = "market_wx_share";
        String key4 = "market_wx_index_brand";
        String key5 = "market_wx_catlog_goods";
        String key6 = "market_wx_catlog_list";
        String key7 = "market_wx_index_hot";

        String value1 = systemMapper.getMarketSystemByKeyName(key1);
        String value2 = systemMapper.getMarketSystemByKeyName(key2);
        String value3 = systemMapper.getMarketSystemByKeyName(key3);
        String value4 = systemMapper.getMarketSystemByKeyName(key4);
        String value5 = systemMapper.getMarketSystemByKeyName(key5);
        String value6 = systemMapper.getMarketSystemByKeyName(key6);
        String value7 = systemMapper.getMarketSystemByKeyName(key7);

        HashMap<String, String> wxResult = new HashMap<>();

        wxResult.put(key1, value1);
        wxResult.put(key2, value2);
        wxResult.put(key3, value3);
        wxResult.put(key4, value4);
        wxResult.put(key5, value5);
        wxResult.put(key6, value6);
        wxResult.put(key7, value7);

        return wxResult;
    }

    /*@Override
    public int postMallConfig(Map map) {

        System system = new System();
        for (Object key : map.keySet()) {

            system.setKeyName((String) key);
            system.setKeyValue((String) map.get(key));
            system.setUpdateTime(new Date());

            systemMapper.setMarketSystemByKeyName(system);
        }

        return 200;

    }*/
}
